package guiCode;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.sun.rowset.CachedRowSetImpl;

import secretary.closeSecretaryScreen;
import secretary.defineSemester;
import student.assignmentInfo;

import flexibility.defineUserRole;
import flexibility.showMessage;
import flexibility.uploadFileInfo;
import flexibility.userData;
import gui.studentScreen;
import gui.uploadManager;
import basic.ClientConsole;

public class studentScreenCode {

 
 

	static Integer myID; // Stores the user's ID
	private static ClientConsole studentConnection;
	public uploadManager uploadMngr;
	public uploadFileInfo fileInfo;
	
	static Integer currentSemesterID;
	private CachedRowSetImpl crs;
	private userData DBData = new userData();
	private showMessage msg=new showMessage();

	/**
	 * This method runs when the user press "Upload Assignment"
	 */
	public void runUploadManager(Integer assignmentID,JFrame studentScreenWindow) {
		Integer fileID;
		
		// Initialize fileInfo
		fileInfo = new uploadFileInfo(); 
		
		//Check if the user already send solution to the specific assignment
		fileID=ifCommited(assignmentID, myID, currentSemesterID); 
		System.out.println("The existed fileID ="+fileID);
		if (fileID!=null) {
			int n = JOptionPane.showConfirmDialog(
				    null,
				    "You have commited this assignment! \n"+"Do you want to replace it?",
				    "Uploading status",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
			if (n==1){
				return;
			}

			fileInfo.setIfUpdate(true);
			fileInfo.setfileID(fileID);
		}
		else {
			fileInfo.setIfUpdate(false);
		}
		
		//Check if the student's commit his assignment after deadline
		if(ifDatePassed(assignmentID)){
			System.out.println("The date of of commit - passed");
			msg.showMessages(studentScreenWindow, "You have passed the commit date of this assignment" ,"Commit Assignment Attention" , 2);
			fileInfo.setIfLate(true);
		}
		else{
			fileInfo.setIfLate(false);
		}
		fileInfo.setAssignmentID(assignmentID);
		fileInfo.setRole(4); //set the role of the user - 4 means 'student'
		fileInfo.setAssignmentCommitDate(this.getCurrentDate()); //set the date of student's submission of the assignment
		fileInfo.setSemesterID(currentSemesterID);
		fileInfo.setStudentID(myID);
		uploadMngr = new uploadManager(studentConnection, myID, fileInfo);

	}

	// Stores the user's ID
	public void setMyID(int id) {
		myID = id;
	}
	
	public Integer getMyID() {
		return myID;
	}

	public void setCurrentSemester(int id) {
		currentSemesterID = id;
	}
	
	public Integer getCurrentSemester(){
		return currentSemesterID;
	}

	/**
	 * Stores the connection to the server
	 * @param conn - connection to server
	 */
	public void setConnection(ClientConsole conn) {
		studentConnection = conn;
	}

	/**
	 * This method checks if the specific assignment was committed before
	 */
	public Integer ifCommited(Integer assignmentID, Integer studentID, Integer currentSemesterID) {

		Integer fileID = null;

		DBData.deleteAll();
		DBData.addData(46); // query to check if the specific assignment was
							// committed by student
		DBData.addData(1);
		DBData.addData(assignmentID);
		DBData.addData(studentID);
		DBData.addData(currentSemesterID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();// this will get the results from
												// the server

		try {
			if (crs.next()) {
				fileID=crs.getInt(1);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return fileID;
	}

	/**
	 * This method returns the current date according to server
	 * 
	 */
	public Date getCurrentDate() {
		Date CurrentDate = null;
		DBData.deleteAll();
		DBData.addData(44);
		DBData.addData(1);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();// this will get the results from
												// the server
		try {
			crs.next();
			CurrentDate = crs.getDate(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return CurrentDate;

	}
	
	/**
	 * This method executes logout and closes the student's window
	 * @return 
	 */
	public void closeStudentWindow(){
		
		closeSecretaryScreen css = new closeSecretaryScreen();
		css.closeSecretearyScreen( myID, studentConnection, DBData);
		
	}
	
	/**
	 * 
	 * @return String [] nameArr - String array of FirstName and SecondName of the student
	 */
	public String[] getStudentName(){
		String [] nameArr = new String[2];
		DBData.deleteAll();
		DBData.addData(172);
		DBData.addData(1);
		DBData.addData(myID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		try {
			crs.next();
			nameArr[0] = crs.getString(1);
			nameArr[1] = crs.getString(2);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nameArr;
	}
	
	/**
	 * This method returns class name of the student
	 */
	public String getClassName(){
		String className=null;
		DBData.deleteAll();
		DBData.addData(48);
		DBData.addData(1);
		DBData.addData(myID);
		DBData.addData(currentSemesterID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		try {
			
			if(crs.next()==false){
			
				System.out.println("The user does not assigned to any class");
			}
			else{
				className = crs.getString(1);
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return className;
	}
	
	/**
	 * This method returns the fileID according to the file that the user wants to choose
	 * @param infoArr - array list of assignmentInfo that stores the full information about the Down Table
	 * @param row - row number that user selected
	 * @param coloumn - column number that user selected
	 * @return
	 */
	public Integer getSelectedFileID(ArrayList<assignmentInfo> infoArr, Integer row, Integer column){
		Integer id=null;
		switch (column){
		case 0:
			id=infoArr.get(row).fileAssignmentID;
			break;
		case 2:
			id=infoArr.get(row).fileStudentID;
			break;
			
		case 3:
			id=infoArr.get(row).fileFeedbackID;
			break;
		case 4:
			id=infoArr.get(row).fileCommentID;
			break;
		}
		return id;
	}
	
	public Integer getSelectedAssignmentID(ArrayList<assignmentInfo> infoArr, Integer row, Integer column){
		
		return infoArr.get(row).assignmentId;
	}
	
	/**
	 * This method checks if the student has missed the date to commit the specific assignemnt
	 * @param assignmentId -assignmentID you want for which you to upload your file 
	 * @return - true or false
	 */
	public boolean ifDatePassed(Integer assignmentId){
		Date toDate;
		Date currDate;
		DBData.deleteAll();
		DBData.addData(121);
		DBData.addData(1);
		DBData.addData(assignmentId);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		try {
			if(crs.next()){
				toDate = crs.getDate(1);
				currDate=this.getCurrentDate();
				if(currDate.after(toDate)){
					return true;
				}
			}
			else{
				return false;
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
}

